import { UserKey, PermissionKey, DictKey, MenuKey } from './constants'

// Token
export function getToken() {
  const token = sessionStorage.getItem(UserKey) || '{}'
  const user = JSON.parse(token) || {}
  return user.token
}

// 用户
export function setUser(user: object = {}) {
  sessionStorage.setItem(UserKey, JSON.stringify(user))
}

export function getUser() {
  const user = sessionStorage.getItem(UserKey) || '{}'
  return JSON.parse(user)
}

// 字典
export function setDicts(dicts: object = {}) {
  localStorage.setItem(DictKey, JSON.stringify(dicts))
}

export function getDicts() {
  const dicts = localStorage.getItem(DictKey) || '{}'
  return JSON.parse(dicts)
}

// 用户权限
export function setPermissions(permissions: Array<object> = []) {
  localStorage.setItem(PermissionKey, JSON.stringify(permissions))
}

export function getPermissions() {
  const permissions = localStorage.getItem(PermissionKey) || '[]'
  return JSON.parse(permissions)
}

// 菜单
export function setMenus(menus: Array<object> = []) {
  localStorage.setItem(MenuKey, JSON.stringify(menus))
}

export function getMenus() {
  const menus = localStorage.getItem(MenuKey) || '[]'
  return JSON.parse(menus)
}

// 清除
export function remove() {
  sessionStorage.removeItem(UserKey)
  localStorage.removeItem(DictKey)
  localStorage.removeItem(PermissionKey)
  localStorage.removeItem(MenuKey)
}
